function send(cb, url) {
    let xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function () {
        if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
            let data = JSON.parse(xhr.responseText);
            cb(data.data == null ? data : data.data);
        }
    }
    xhr.open("GET", url == null ? "http://localhost:8081/csap/apidoc" : (url.replace("/csap/apidoc", "")) + "/csap/apidoc", true);
    xhr.setRequestHeader("Content-Type", "application/json")
    xhr.send();
}

new Vue({
    el: '#app',
    data() {
        return {
            apiInfo: {},
            apiList: [],
            resources: [],
            groups: [],
            versions: [],
            /**
             * 当前版本号
             */
            version: "default",
            /**
             * 当前分组
             */
            group: "default",
            /**
             * API
             */
            groupApi: "",
            searchValue: "",
            change: false
        }
    },
    mounted() {
        this.requestDoc();
    },

    methods: {
        //请求
        requestDoc() {
            send((r) => {
                this.apiInfo = r.apiInfo;
                this.apiList = r.apiList;
                if (!this.change) {
                    this.resources = r.resources;
                }
                this.groups = r.groups;
                this.versions = r.versions;
                this.$nextTick(() => {
                    document.querySelector("#app").removeAttribute("hidden");
                    document.querySelector("#loadDiv").setAttribute("hidden", "true");
                    window.onscroll = () => {
                        this.setScroll();
                    };
                })
            }, this.url())
        },
        setScroll() {
            let titles = document.getElementsByClassName('api-title');
            let scrollTop = document.documentElement.scrollTop;
            let navAs = document.getElementsByClassName('nav-a');
            for (let j = navAs.length; j > -1; j--) {
                if (navAs[j - 1]) {
                    navAs[j - 1].className = "nav-a splitStr"
                    if ("api" == navAs[j - 1].getAttribute("type")) {
                        navAs[j - 1].classList.add("api-left-title");
                    }
                }
            }
            for (let i = titles.length; i > -1; i--) {
                let currentTarget = titles[i - 1];
                if (currentTarget && (scrollTop >= currentTarget.offsetTop)) {
                    let nav = document.querySelector("a[href='#" + currentTarget.id + "']");
                    if (nav) {
                        let navOffsetTop = nav.offsetTop;
                        document.querySelector(".nav-ul").scrollTop = navOffsetTop;
                        nav.className = "nav-a active splitStr";
                        if ("api" == nav.getAttribute("type")) {
                            nav.classList.add("api-left-title");
                        }
                    }
                    break;
                }
            }
            ;
        },
        /**
         * 全文检索
         * @param e
         */
        search(e) {

        },
        /**
         * 切换API
         * @param e
         */
        changeApi(e) {
            let value = e.currentTarget.selectedOptions[0].value;
            if (value == null || value.length == 0) {
                return;
            }
            this.change = true;
            this.groupApi = value;
            document.querySelector("#loadDiv").removeAttribute("hidden");
            this.requestDoc();
        },
        /**
         * 搜索分组API
         * @param e
         */
        searchGroupApi(e) {
            this.group = e.currentTarget.selectedOptions[0].value;
            document.querySelector("#loadDiv").removeAttribute("hidden");
            setTimeout(() => {
                document.querySelector("#loadDiv").setAttribute("hidden", "true");
            }, 1000)
        },
        /**
         * 搜索版本API
         * @param e
         */
        searchVersionApi(e) {
            this.version = e.currentTarget.selectedOptions[0].value;
            document.querySelector("#loadDiv").removeAttribute("hidden");
            setTimeout(() => {
                document.querySelector("#loadDiv").setAttribute("hidden", "true");
            }, 1000)
        },
        /**
         * 合并model属性
         * @param api
         */
        mareModelField(method) {
            if (method.request != null && method.request.length > 1) {
                for (let x = 1; x < method.request.length; x++) {
                    if (method.request[x].parameters != null && method.request[x].parameters.length > 0) {
                        method.request[0].parameters = method.request[0].parameters.concat(method.request[x].parameters);
                    }
                }
                method.request = method.request.splice(0, 1);
            }
        },
        /**
         * 获取URL
         * @returns {string}
         */
        url() {
            // return "http://192.168.0.20:8980/csap/apidoc";
            return this.groupApi.length == 0 ? window.location.origin + window.location.pathname.replace("/csap-api.html", "") : window.location.origin + this.groupApi.replace("/csap/apidoc", "");
        },
        getModelName(api, method, req) {
            this.$nextTick(() => {
                let el = document.querySelector("table[name='" + (api.name + method.name + req.name) + "']");
                if (el != null && el.previousElementSibling != null) {
                    if (req.value != null && el.getAttribute("name") != "title") {
                        el.outerHTML = el.outerHTML + "<h4 name='title'>" + req.value + "</h4>";
                    }
                }
            });
        },
        /**
         * 获取返回的参数
         * @param parameters 返回参数
         * @param html
         * @param x
         * @returns {string}
         */
        getResponseParams(parameters, x, html) {
            if (html == null) {
                html = "";
            }
            if (x == null) {
                x = 0;
            }
            if (parameters == null || parameters.length == 0) {
                return "";
            }
            for (let parameter of parameters) {
                if (!parameter.force) {
                    if (!((this.group == 'default' || parameter.group.indexOf(this.group) != -1) && (this.version == 'default' || parameter.version.indexOf(this.version) != -1))) {
                        continue;
                    }
                }
                let tr = document.createElement("tr");
                let name = document.createElement("td");
                name.setAttribute("key", "name")
                //左边间隔
                let span = document.createElement("span");
                span.setAttribute("style", "padding-left: " + x * 10 + "px");
                //样式
                let span2 = document.createElement("span");
                if (x > 0) {
                    span2.setAttribute("style", "color: #000");
                    span2.innerText = "↳";
                }
                span.innerHTML = parameter.name == null ? "" : span2.outerHTML + parameter.name;
                name.appendChild(span);
                let dataType = document.createElement("td");
                let dataTyeStr = parameter.dataType;
                if (parameter.length > 0) {
                    dataTyeStr += "(" + parameter.length;
                    if (parameter.decimals > 0) {
                        dataTyeStr += "," + parameter.decimals;
                    }
                    dataTyeStr += ")";
                }
                dataType.innerText = parameter.dataType == null ? "" : dataTyeStr;
                dataType.setAttribute("key", "dataType")
                let required = document.createElement("td");
                required.innerText = parameter.required == null ? "" : parameter.required;
                required.setAttribute("key", "required")
                let defaultValue = document.createElement("td");
                defaultValue.innerText = parameter.defaultValue == null ? "" : parameter.defaultValue;
                defaultValue.setAttribute("key", "defaultValue")

                let example = document.createElement("td");
                example.innerText = parameter.example == null ? "" : parameter.example;
                example.setAttribute("key", "example")
                let value = document.createElement("td");
                value.innerText = parameter.value == null ? "" : parameter.value;
                value.setAttribute("key", "value")

                let description = document.createElement("td");
                description.innerText = parameter.description == null ? "" : parameter.description;
                description.setAttribute("key", "description")

                let group = document.createElement("td");
                group.innerText = parameter.group == null ? "" : parameter.group;
                group.setAttribute("key", "group")

                let version = document.createElement("td");
                version.innerText = parameter.version == null ? "" : parameter.version;
                version.setAttribute("key", "version")

                tr.appendChild(name);
                tr.appendChild(dataType);
                tr.appendChild(required);
                tr.appendChild(defaultValue);
                tr.appendChild(example);
                tr.appendChild(value);
                tr.appendChild(description);
                tr.appendChild(group);
                tr.appendChild(version);
                html += tr.outerHTML;
                if (parameter.children != null && (parameter.children.parameters != null && parameter.children.parameters.length > 0)) {
                    html = this.getResponseParams(parameter.children.parameters, x + 1, html);
                }
            }
            return html;
        }

    }
})
